<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/custom.form.css">
    <style>
        .Impact{
            background-color: #6a79d6;
        }
    </style>
</head>
<body>
<div class="panel panel-default operation_user" hidden>
    <div class="panel-heading title"></div>
    <div class="layui-card-body">
        <form class="layui-form " action="" lay-filter="deptInfo" style="width: 700px;margin-top: 10px">
            <div class="layui-form-item" id="id" hidden>
                <label class="layui-form-label">ID</label>
                <div class="layui-input-block">
                    <input type="userId" name="userId" placeholder="请输入标识ID" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-block">
                    <input type="username" name="username" placeholder="请输入账号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-label ">密码</div>
                <div class="layui-input-block">
                    <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-label ">科室</div>
                <div class="layui-input-block">
                    <input type="userType" name="userType" placeholder="请输入科室" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-label ">性别</div>
                <div class="layui-input-block">
                    <input type="sex" name="sex" placeholder="请输入性别" autocomplete="off" class="layui-input"
                           readonly="readonly" style="background:#eeeeee!important">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="status" lay-skin="switch" lay-filter="switch" lay-text="启用|禁用" checked>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn Impact" lay-submit="" lay-filter="submit">保存</button>
                    <button class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>
<!-- 左树 -->
<div class="layui-fluid">
<!--    <div class="layui-row layui-col-space15">-->
<!--&lt;!&ndash;        <div class="layui-col-sm12 layui-col-md4 layui-col-lg2">&ndash;&gt;-->
<!--&lt;!&ndash;            <div class="layui-card">&ndash;&gt;-->
<!--&lt;!&ndash;                <div class="layui-card-body mini-bar" id="leftTree">&ndash;&gt;-->
<!--&lt;!&ndash;                </div>&ndash;&gt;-->
<!--&lt;!&ndash;            </div>&ndash;&gt;-->
<!--&lt;!&ndash;        </div>&ndash;&gt;-->
<!--        <div class="layui-col-sm12 layui-col-md8 layui-col-lg10">-->
            <div class="user_table_div">
                <div id="searchParam" shiro:hasPermission="sys:user:list">
                    <div class="layui-form-item">
                        <div class="layui-input-inline">
                            <input type="text" id="username" class="layui-input" autocomplete="off" placeholder="请输入账号">
                        </div>
<!--                        <div class="layui-input-inline">-->
<!--                            <input type="text" id="nickName" class="layui-input" autocomplete="off" placeholder="用户名">-->
<!--                        </div>-->
                        <div class="layui-input-inline">
                            <input type="text" readonly class="layui-input" id="createTime" placeholder="创建时间">
                        </div>
                        <div class="layui-input-inline layui-form">
                            <select id="status">
                                <option value="">请选择账号状态</option>
                                <option value="1">正常</option>
                                <option value="2">禁用</option>
                            </select>
                        </div>
                        <div class="layui-input-inline ">
                            <button class="layui-btn Impact" onclick="search()" id="search">搜索</button>
                        </div>
                    </div>

                </div>
                <table class="layui-hide" id="user_table" lay-filter="user_table"></table>
            </div>

        </div>
<!--    </div>-->
<!--</div>-->

<div id="roles" class="demo-transfer" style="display: none"></div>
</body>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm Impact" lay-event="addByImport" shiro:hasPermission="sys:user:add">导入用户</button>
        <button class="layui-btn layui-btn-sm Impact" lay-event="addNewUser" shiro:hasPermission="sys:user:add">添加</button>
        <button class="layui-btn layui-btn-sm Impact" lay-event="batchDeleted" shiro:hasPermission="sys:user:deleted">删除</button>
        <input type="file" id="btn_file" accept=".xlsx" style="display: none">
    </div>
</script>
<script type="text/html" id="tool">
<!--    <a class="layui-btn  layui-btn-xs" lay-event="operationRole" shiro:hasPermission="sys:user:role:update">赋予角色</a>-->
    <a class="layui-btn layui-btn-xs Impact" lay-event="edit" shiro:hasPermission="sys:user:update">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" shiro:hasPermission="sys:user:deleted">删除</a>
</script>
<script src="/layui/layui.all.js"></script>
<script src="/js/sheetJson.js"></script>
<script src="/js/core.util.js"></script>
<script>
    //获取token
    var token = CoreUtil.getData("access_token");
    //地址栏转义token中的#号
    var tokenQuery = token.replace("#", "%23");
    var tableIns1;
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var laydate = layui.laydate;
    var tree = layui.tree;
    var transfer = layui.transfer;
    var deptNo = null;
    var startTime = null;
    var endTime = null;
    layui.use(['table', 'layer', 'laydate', 'tree', 'transfer'], function () {

        // initLeftTree();

        //加载table
        tableIns1 = table.render({
            elem: '#user_table'
            , contentType: 'application/json'
            , headers: {"authorization": token}
            , page: true //开启分页
            , url: '/sys/users' //数据接口
            , method: 'POST'
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                console.log(JSON.stringify(res.data))
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                    "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
                }
            }
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'userId', title: 'ID', width: 130},
                    {field: 'username', title: '账号', width: 130},
                    {field: 'password', title: '密码', width: 130},
                    {field: 'userId', title: 'id', width: 120},
                    {
                        field: 'userType', title: '身份', width: 120, templet:function (item) {
                            switch(item.userType) {
                                case 1:
                                    return "管理员"
                                case 11:
                                    return "眼科";
                                case 12:
                                    return "耳鼻喉科"
                                case 13:
                                    return "口腔科"
                                case 14:
                                    return "外科"
                                case 15:
                                    return "血压脉搏科"
                                case 16:
                                    return "内科"
                                case 17:
                                    return "化验科"
                                case 18:
                                    return "胸部放射科"
                                case 19:
                                    return "其他"
                                case 20:
                                    return "体检负责"
                                case 21:
                                    return "体检负责医师"
                                case 22:
                                    return "医院领导"
                            }
                        }
                    },
                    {
                        field: 'sex', title: '性别', width: 100, templet: function (item) {
                            if (item.sex === 1) {
                                return "男";
                            }
                            if (item.sex === 2) {
                                return "女";
                            }
                        }
                    },
                    {field: 'createTime', title: '创建时间', minWidth: 120},
                    {
                        field: 'status', title: '状态', width: 100, templet: function (item) {
                            if (item.status === 1) {
                                return '  <input type="checkbox" lay-skin="switch" lay-text="正常|禁用" checked disabled>';
                            }
                            if (item.status === 2) {
                                return '  <input type="checkbox" lay-skin="switch" lay-text="正常|禁用" disabled>';
                            }
                        }
                    },
                    {width: 300, toolbar: "#tool", title: '操作'}
                ]
            ]
            , toolbar: '#toolbar'
        });

        laydate.render({
            elem: '#createTime'
            , type: 'datetime'
            , range: '~'
            , done: function (value, date, endDate) {
                if (value != null && value != undefined && value != "") {
                    startTime = value.split("~")[0];
                    endTime = value.split("~")[1];
                } else {
                    startTime = null;
                    endTime = null;
                }

            }
        });

        table.on('toolbar(user_table)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'batchDeleted':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length == 0) {
                        layer.msg("请选择要批量删除的用户");
                    } else {
                        var userIds = [];
                        $(data).each(function (index, item) {
                            userIds.push(item.userId);
                        });
                        tipDialog(userIds, "选中的");
                    }
                    break;
                case 'addNewUser':
                    $(".title").html("新增用户");
                    $(".user_table_div").hide();
                    $(".operation_user").show();
                    $(".operation_user #id").hide();
                    $(".operation_user input[name=username]").val("");
                    $(".operation_user input[name=password]").val("");
                    $(".operation_user input[name=userType]").val("");
                    $(".operation_user input[name=sex]").val("");
                    $(".operation_user input[name=status]").attr('checked', 'checked');
                    $(".operation_user input[name=status]").attr('type', 'hidden').val(1);
                    // var x = document.getElementsByClassName("layui-unselect layui-form-switch");
                    // x[0].setAttribute("class", "layui-unselect layui-form-switch layui-form-onswitch");
                    // var d = document.getElementsByTagName('em')[0];
                    // d.firstChild.nodeValue = '启用';
                    form.render(); //更新全部
                    break;

                case 'addByImport':
                    console.log('no')
                    let file = document.getElementById('btn_file')
                    file.click();
                    file.onchange = function (){
                        console.log('yes')
                        sheetJson.readWorkbookFromLocalFile(file.files[0],function (json){
                            console.log(json)
                            console.log('successfully upload')
                            // let json1 ='[{"doctorId":"3","doctorName":"dadada","doctorCard":"123312","doctorDepartment":"外科"}]'
                            $.post({
                                type: 'POST',
                                url: 'importDoctor',
                                dataType: "JSON",
                                data: JSON.stringify(json),
                                contentType: "application/json",
                                success: function (res) {
                                    if (res.code === 0) {
                                        layer.msg('录入信息成功', function () {
                                            console.log("success")
                                        });
                                    } else {
                                        layer.msg('录入信息失败');

                                    }
                                }
                            })
                            return false;
                        });
                    }
                    break;

            }
        });
        table.on('tool(user_table)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    var userIds = [];
                    userIds.push(data.userId);
                    tipDialog(userIds, data.username);
                    break;
                case 'edit':
                    $(".user_table_div").hide();
                    $(".operation_user").show();
                    $(".title").html("编辑用户:"+data.username);
                    $(".operation_user input[name=userId]").val(data.userId);
                    $(".operation_user #id").show()
                    $(".operation_user input[name=userId]").attr("disabled","disabled")
                    $(".operation_user input[name=username]").val(data.username);
                    $(".operation_user input[name=password]").val("");
                    $(".operation_user input[name=userType]").val(data.userType);
                    $(".operation_user input[name=sex]").val(data.sex);
                    if (data.status === 1) {
                        $(".operation_user input[name=status]").attr('checked', 'checked');
                        $(".operation_user input[name=status]").attr('type', 'hidden').val(1);
                    } else{
                    $(".operation_user input[name=status]").attr('type', 'hidden').removeAttr("checked").val(2);}
                    //     var x = document.getElementsByClassName("layui-unselect layui-form-switch");
                    //     x[0].setAttribute("class", "layui-unselect layui-form-switch");
                    //     var d = document.getElementsByTagName('em')[0];
                    //     d.firstChild.nodeValue = '禁用';
                    // }
                    form.render(); //更新全部
                    break;
                // case 'operationRole':
                //     CoreUtil.sendGet("/sys/user/roles/" + data.userId, null, function (res) {
                //         initTransfer(res.data);
                //         layer.open({
                //             type: 1,
                //             offset: '50px',
                //             skin: 'layui-layer-molv',
                //             title: "赋予角色",
                //             area: ['500px'],
                //             shade: 0,
                //             shadeClose: false,
                //             content: jQuery("#roles"),
                //             btn: ['确定', '取消'],
                //             yes: function (index) {
                //                 //获得右侧数据
                //                 var roleIds = [];
                //                 var getData = transfer.getData('ownData');
                //                 if (getData.length == 0) {
                //                     layer.msg("请选择要赋予用户的角色");
                //                 } else {
                //                     $(getData).each(function (index, item) {
                //                         roleIds.push(item.value);
                //                     });
                //                 }
                //                 CoreUtil.sendPost("/sys/user/info" ,data.field,function (res) {
                //                     layer.msg(res.msg);
                //                     search()
                //                 });
                //
                //                 layer.close(index);
                //             }
                //         });
                //     });
                //     break;
            }
        });
        $("#btn_cancel").click(function () {
            $(".user_table_div").show();
            $(".operation_user").hide();
            return false;
        });
        var tipDialog = function (userIds, username) {
            layer.open({
                content: '确定要删除' + username + "用户么?",
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendDelete("/sys/user", userIds, function (res) {
                        layer.msg(res.msg);
                        search()
                    });
                }
            });
        };
        form.on('switch(switch)', function () {
            $(".operation_user input[name=status]").attr('type', 'hidden').val(this.checked ? 1 : 2);

        });
        form.on('submit(submit)', function (data) {
            if (data.field.userId === undefined || data.field.userId === null || data.field.userId === "") {
                CoreUtil.sendPost("/sys/user", data.field, function (res) {
                    $(".user_table_div").show();
                    $(".operation_user").hide();
                    search()
                });
            } else {
                console.log(JSON.stringify(data.field))
                CoreUtil.sendPost("/sys/user/info", data.field, function (res) {
                    $(".user_table_div").show();
                    $(".operation_user").hide();
                    search()
                });
            }
            return false;
        });

        // $(".operation_user input[name=deptName]").click(function () {
        //     layer.open({
        //         type: 1,
        //         skin: 'layui-layer-molv',
        //         title: "选择部门",
        //         area: ['400px', '450px'],
        //         shade: 0,
        //         shadeClose: false,
        //         content: jQuery("#deptTree"),
        //         btn: ['确定', '取消'],
        //         yes: function (index) {
        //             if (selectNode.data != null) {
        //                 //选中回显
        //                 $(".operation_user input[name=deptId]").val(selectNode.data.id);
        //                 $(".operation_user input[name=deptName]").val(selectNode.data.title);
        //             }
        //
        //             layer.close(index);
        //         }
        //     });
        // });

        // function initLeftTree(){
        //     CoreUtil.sendGet("/sys/dept/tree", null, function (res) {
        //         loadLeftTree(res.data);
        //         loadDeptTree(res.data);
        //     });
        // }
        var initTransfer = function (data) {
            transfer.render({
                elem: '#roles'
                , data: data.allRole
                , title: ['赋予角色']
                , showSearch: true
                , value: data.ownRoles
                , id: 'ownData'
                , parseData: function (res) {
                    return {
                        "value": res.id //数据值
                        , "title": res.name //数据标题
                    }
                }
            })
        }
    });
    //执行查询
    function search() {
        //这里以搜索为例

        tableIns1.reload({
            where: { //设定异步数据接口的额外参数，任意设

                username: $("#username").val(),
                startTime: startTime,
                endTime: endTime,
                status: $("#status").val(),
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    }
</script>
</html>